﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Warehouse_Management.New;

namespace Warehouse_Management
{
    public partial class frmDanhMucKho : DevExpress.XtraEditors.XtraForm
    {
        public frmDanhMucKho()
        {
            InitializeComponent();
        }
        public int status = 0;
        private DataTable dt;
        private void frmDanhMucKho_Load(object sender, EventArgs e)
        {
            loadData();
        }
        private void loadData()
        {
            dt = KhoHang.SelectAll().Tables[0];
            gridControl1.DataSource = dt;
        }
        public void EditKho()
        {
            int RowHandle = gridView1.GetSelectedRows()[0];
            if (RowHandle >= 0)
            {
                int index = gridView1.GetDataSourceRowIndex(RowHandle);
                KhoHang Kho = GetKho(index);
                if (Kho != null)
                {
                    bool khoEdit = true;
                    string MaKho = Kho.MaKho;
                    while (khoEdit)
                    {
                        frmKho f = new frmKho();
                        f.KHO = Kho;
                        f.type = frmKho.OpenType.IsEdit;
                        f.ShowDialog(this);
                        if (f.isSave)
                        {

                            if (Kho.MaKho == MaKho || CheckKho(Kho.MaKho))
                            {
                                DataRow dr = dt.Rows[index];
                                dr["MaKho"] = Kho.MaKho;
                                dr["TenKho"] = Kho.TenKho;
                                dr["GhiChu"] = Kho.GhiChu;
                                dr["DiaChi"] = Kho.DiaChi;
                                khoEdit = false;
                                this.Text = "Danh mục nhà sách" + "(*)";
                            }
                            else
                            {
                                khoEdit = MessageBox.Show("Mã nhà sách đã tồn tại, Bạn có muốn nhập lại ?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes;
                            }
                        }
                        else
                            khoEdit = false;
                    }
                   

                }
            }

        }
        private KhoHang GetKho(int index)
        {
            KhoHang Kho = new KhoHang();
            DataRow dr = dt.Rows[index];
            if (dr != null)
            {
                Kho.MaKho = dr["MaKho"].ToString();
                Kho.TenKho = dr["TenKho"].ToString();
                Kho.GhiChu = dr["GhiChu"].ToString();
                Kho.DiaChi = dr["DiaChi"].ToString();
                return Kho;
            }
            else
                return null;
        }
        private bool CheckKho(string MaKho)
        {
            DataRow[] dr = dt.Select("MaKho = '" + MaKho + "'");
            if (dr.Length > 0) return false;
            else
                return true;
        }
        public void Delete()
        {
            int[] rowsHandle = gridView1.GetSelectedRows();
            for (int i = 0; i < rowsHandle.Length; i++)
            {
                dt.Rows[gridView1.GetDataSourceRowIndex(rowsHandle[i])].Delete();
            }
            if (rowsHandle.Length > 0)
                this.Text = "Danh mục nhà sách" + "(*)";
            gridControl1.Refresh();
            gridView1.RefreshData();

        }
        public void NewHang()
        {
            KhoHang Kho = new KhoHang();
            bool hangnew = true;
            try
            {
                while (hangnew)
                {
                    frmKho f = new frmKho();
                    f.KHO = Kho;
                    f.type = frmKho.OpenType.IsNew;
                    f.ShowDialog(this);
                    if (f.isSave)
                    {
                        Kho = f.KHO;
                        if (CheckKho(Kho.MaKho))
                        {
                            DataRow dr = dt.NewRow();
                            dr["MaKho"] = Kho.MaKho;
                            dr["TenKho"] = Kho.TenKho;
                            dr["GhiChu"] = Kho.GhiChu;
                            dr["DiaChi"] = Kho.DiaChi;
                            dt.Rows.Add(dr);
                            hangnew = false;
                            this.Text = "Danh mục nhà sách" + "(*)";
                        }
                        else
                        {
                            hangnew = MessageBox.Show("Mã nhà sách đã tồn tại, Bạn có muốn nhập lại ?", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes;
                        }
                    }
                    else
                        hangnew = false;
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hit = gridView1.CalcHitInfo((e as MouseEventArgs).Location);
            if (hit.InRow)
            {
                gridView1.SelectRow(hit.RowHandle);
                this.EditKho();
            }
        }
        private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                DataRow dr = gridView1.GetDataRow(e.RowHandle);
                if (dr != null)
                {
                    switch (dr.RowState)
                    {
                        case DataRowState.Added:
                            e.Appearance.BackColor = Color.Green;
                            break;
                        case DataRowState.Modified:
                            e.Appearance.BackColor = Color.Yellow;
                            break;
                        case DataRowState.Deleted:
                            e.Appearance.BackColor = Color.Red;
                            break;
                    }
                }

            }
        }
        private void frmDanhMucKho_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.Text.Contains("(*)"))
                if (MessageBox.Show("Bạn có muốn lưu thông tin trước khi đóng ? ", "Thông báo", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    this.Save();

        }
        public void Save()
        {
            try
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    if (dr.RowState != DataRowState.Unchanged)
                    {
                        KhoHang Kh = new KhoHang();
                        if (dr.RowState == DataRowState.Deleted)
                        {
                            Kh.MaKho = dr["MaKho",DataRowVersion.Original].ToString();
                            Kh.TenKho = dr["TenKho",DataRowVersion.Original].ToString();
                            Kh.GhiChu = dr["GhiChu",DataRowVersion.Original].ToString();
                            Kh.DiaChi = dr["DiaChi",DataRowVersion.Original].ToString();
                            Kh.Delete();
                        }
                        else
                        {
                            Kh = GetKho(i);
                            Kh.InsertUpdate();
                        }
                      

                    }
                }
                MessageBox.Show("Lưu thành công");
                this.Text = "Danh mục nhà sách";
                try
                {
                    gridView1.RefreshData();
                    dt.AcceptChanges();
                }
                catch
                {

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi khi lưu thông tin danh mục hàng hóa. " + ex.Message);


            }
        }
    }
}